package net.whg.service.impl;

import net.whg.annotaion.TargetDataSource;
import net.whg.datasource.DataSourceType;
import net.whg.feign.UserFeignService;
import net.whg.mapper.ProductMapper;
import net.whg.model.ProductDO;
import net.whg.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductMapper productMapper;

    @Autowired
    private UserFeignService userFeignService;


    @Override
    public void initProduct() {
        Random random = new Random();
        ProductDO productDO = ProductDO.builder().title("产品" + random.nextInt(100)).build();
        productMapper.insert(productDO);
    }

    @Override
    public void testFeign() {
        initProduct();
        try{
            userFeignService.init();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    @TargetDataSource(value = DataSourceType.MASTER)
    public void save() {
        Random random = new Random();
        ProductDO productDO = ProductDO.builder().title("产品" + random.nextInt(100)).build();
        //DynamicDataSource.name.set(DataSourceType.MASTER);
        productMapper.insert(productDO);
    }

    @Override
    @TargetDataSource(value = DataSourceType.SLAVE)
    public List<ProductDO> list() {
        //DynamicDataSource.name.set(DataSourceType.SLAVE);
        return productMapper.selectList(null);
    }
}
